<?php

register_shutdown_function(function() {
            $lastError = error_get_last();
            if (!empty($lastError) && $lastError['type'] == E_ERROR) {
                http_response_code(500);
            }
        });

session_start();
include_once 'objects/User.inc';
include_once 'objects/Cliente.inc';
include_once 'objects/Servicio.inc';
include_once 'objects/CFinal.inc';
include_once 'objects/CFiscal.inc';
include_once 'objects/DCFinal.inc';
include_once 'objects/DCFiscal.inc';

if (isset($_SESSION['user'])) {
    $user = unserialize($_SESSION['user']);
} else {
    $user = new User();
}

function delete($id, $type) {
    switch ($type) {
        case "cliente":
            $obj = new Cliente();
            break;
        case "servicio":
            $obj = new Servicio();
            break;
        case "usuario":
            $obj = new User();
            break;
        case "cfinal":
            $obj = new CFinal();
            break;
        case "cfiscal":
            $obj = new CFiscal();
            break;
        case "dcfinal":
            $obj = new DCFinal();
            break;
        case "dcfiscal":
            $obj = new DCFiscal();
            break;
        default :
            http_response_code(404);
            return "404 Not Found Type: $type";
    }
    if (!$obj->deleteE($id)) {
        http_response_code(500);
        return $obj->msg;
    }
}

if (isset($_POST["id"]) && isset($_POST["type"])) {
    if (!$user->isLogged()) {
        http_response_code(403);
        exit();
    }
    echo delete($_POST["id"], $_POST["type"]);
} else {
    if (!$user->isLogged()) {
        header("Location: menu.php");
    } else {
        header("Location: index.php");
    }
}
?>
